﻿using System;
using System.Collections.Generic;
using System.Text;
using EP.Transport;

namespace EP.Packet
{
    enum EP_Version {Zero, Zero64}; // Must not contain more than 8 elems to fit in EP header.

    class EP_Packet
    {
        private EP_Adress destination;
        private EP_Adress source;
        private TransportPacket trasportPacket;
        private EP_Version version;

        #region Properties
        public TransportPacket TransportPacket
        {
            get
            {
                return this.trasportPacket;
            }
        }

        public EP_Version Version
        {
            get
            {
                return this.version;
            }
        }

        public int TotalSize
        {
            get
            {
                return Constants.EP_HEADER_AND_FOOTER_SIZE + this.trasportPacket.TotalByteSize;
            }
        }

        public EP_Adress Destination
        {
            get
            {
                return this.destination;
            }
        }
        
        public EP_Adress Source
        {
            get
            {
                return this.source;
            }
        }
        #endregion

        public EP_Packet(EP_Version ver, TransportPacket tpMsg, EP_Adress source, EP_Adress destination)
        {
            this.version = ver;
            this.trasportPacket = tpMsg;
            this.source = source;
            this.destination = destination;
        }

        public EP_Packet(byte[] packet, TransportProtocolHandler tspH)
        {
            // Check checksum and convert. Exception if wrong version or checksum
            throw new NotImplementedException();
        }

        private struct Fletcher16bitChecksum
        {
            public byte A;
            public byte B;

            public Fletcher16bitChecksum(byte a, byte b)
            {
                this.A = a;
                this.B = b;
            }
        }

        /// <summary>
        /// Förklara djungel med 8, 16 osv 1 bit komplement??. Ref till implentation. Motivera val av
        /// fletcher (snabbhet och korrekthet). Ej så korrekt om stora delar är fel.
        /// 
        /// According to original paper by Fletcher [1], simplified the ammount of undetected errors
        /// are in the magnitude of 0.001 % of all errors. However, the fletcher sum can not destinguish
        /// bytes of all 00 and all FF.
        /// 
        /// [1] Fletcher, J., "An Arithmetic Checksum for Serial Transmissions,"
        ///     Communications, IEEE Transactions on , vol.30, no.1, pp. 247-252, Jan 1982
        ///     URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1095369&isnumber=23952
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="size">Number of elements in array data</param>
        /// <returns>The fletcher-8 checksum(16 bits) of data array. Avgör korrekt? Eller skicka...</returns>
        private Fletcher16bitChecksum fletcher8(byte[] data, int size){
            throw new NotImplementedException();
        }

        public static bool IsCompletePacketSize(byte[] packet, int size)
        {
            if (size < Constants.EP_HEADER_AND_FOOTER_SIZE)
            {
                return false;
            }
            else if (size == (int)packet[Constants.EP_INDEX_OF_PACKET_SIZE])
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public byte[] Serialize()
        {
            // Convert and calculate checksum
            // Checksum Fletcher 4 (8 bit checksum)??
            throw new NotImplementedException();
        }
    }
}
